package com.jianggujin.zkgui;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NodeAddDialog extends JDialog {
    private JTextField connectString;
    private JTextField name;

    private JButton saveBtn;
    private ConfigItemChooserDialog parent;

    private int row = -1;

    private NodeAddDialog(ConfigItemChooserDialog parent) {
        super(parent);
        this.parent = parent;
        initComponent();
        initDialog();
    }

    private NodeAddDialog(ConfigItemChooserDialog parent, int row, ConfigItem configItem) {
        super(parent);
        this.parent = parent;
        initComponent();
        this.row = row;
        this.name.setText(configItem.getName());
        this.connectString.setText(configItem.getConnectString());
        initDialog();
    }

    private void initComponent() {
        Box center = Box.createVerticalBox();
        center.add(Box.createVerticalStrut(10));

        Box up = Box.createHorizontalBox();
        up.add(Box.createHorizontalStrut(10));
        up.add(new JLabel("地址："));
        up.add(Box.createHorizontalStrut(10));
        connectString = new JTextField("127.0.0.1:2181");
        up.add(connectString);
        up.add(Box.createHorizontalStrut(10));
        center.add(up);

        center.add(Box.createVerticalStrut(10));

        Box down = Box.createHorizontalBox();
        down.add(Box.createHorizontalStrut(10));
        down.add(new JLabel("名称："));
        down.add(Box.createHorizontalStrut(10));
        name = new JTextField();
        down.add(name);
        down.add(Box.createHorizontalStrut(10));
        center.add(down);

        this.add(center);

        saveBtn = new JButton("保存");
        saveBtn.addActionListener(e -> {
            String connectString = this.connectString.getText();
            if (connectString.length() == 0) {
                JOptionPane.showMessageDialog(NodeAddDialog.this, "地址必填");
                return;
            }
            String name = this.name.getText();
            if (name.length() == 0) {
                JOptionPane.showMessageDialog(NodeAddDialog.this, "名称必填");
                return;
            }
            ConfigItem item = new ConfigItem();
            item.setConnectString(connectString);
            item.setName(name);
            if (this.row > -1) {
                parent.updateConfigItem(row, item);
            } else {
                parent.addConfigItem(item);
            }
            this.dispose();
        });
        JPanel buttom = new JPanel();
        buttom.add(saveBtn);
        this.add(buttom, BorderLayout.SOUTH);
    }

    private void initDialog() {
        setTitle("连接属性配置");
        setModal(true);
        setAlwaysOnTop(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(500, 150);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 设置窗体在显示器居中显示
        this.setBounds((width - getWidth()) / 2, (height - getHeight()) / 2, getWidth(), getHeight());
        setResizable(false);
        this.setVisible(true);
    }
}
